import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import util from '../libs/util'

const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return VueRouterPush.call(this, location).catch(err => err)
}
const VueRouterReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace (location) {
  return VueRouterReplace.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/cow/index',
  },
  {
    path: '/cow',
    name: 'Home',
    component: Home,
    meta: { auth: true },
    children: [
      {
        path: 'index',
        name: 'Index',
        meta: {
          auth: true,
          title: '首页',
          keepAlive:true
        },
        component: () => import(/* webpackChunkName: "Index" */ '../views/CowIndex.vue')
      },

      {
        path: 'category',
        name: 'CategoryManager',
        meta: {
          auth: true,
          title: '分类',
          keepAlive:true
        },
        component: () => import(/* webpackChunkName: "CategoryManager" */ '../views/CategoryManager.vue')
      },

      {
        path: 'friend',
        name: 'Friend',
        meta: {
          auth: true,
          title: '友链',
          keepAlive:true
        },
        component: () => import(/* webpackChunkName: "Friend" */ '../views/FriendManager.vue')
      },

      {
        path: 'article',
        name: 'ArticleManager',
        meta: {
          auth: true,
          title: '文章',
          keepAlive:true
        },
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "ArticleManager" */ '../views/ArticleManager.vue')
      },
    ]
  },

  {
    path: '/article',
    name: 'CreateArticle',
    meta: {
      auth: true,
      title: '创建'
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "CreateArticle" */ '../views/EditArticle.vue')
  },
  {
    path: '/article/:id',
    name: 'UpdateArticle',
    meta: {
      auth: true,
      title: '编辑'
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "CreateArticle" */ '../views/EditArticle.vue')
  },
  {
    path: '/login',
    name: 'Login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "Login" */ '../views/Login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

/**
 * 路由拦截
 * 权限验证
 */
router.beforeEach(async (to, from, next) => {
  // 验证当前路由所有的匹配中是否需要有登录验证的
  if (to.matched.some(r => r.meta.auth)) {
    // 这里暂时将cookie里是否存有token作为验证是否登录的条件
    // 请根据自身业务需要修改
    const token = util.cookies.get('token')
    if (token && token !== 'undefined') {
      next()
    } else {
      // 没有登录的时候跳转到登录界面
      // 携带上登陆成功之后需要跳转的页面完整路径
      next({
        name: 'Login',
        query: {
          redirect: to.fullPath
        }
      })
    }
  } else {
    // 不需要身份校验 直接通过
    next()
  }
})

router.afterEach(to => {
  // 更改标题
  util.title(to.meta.title)
})

export default router
